﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Itop.GpsCar.BLL;
using Itop.GpsCar.Model;
using Tools;
using Itop.GpsCar.Web;

namespace Web.Admin.Core
{
    public partial class UserMgr : WMS_UI
    {
        //信息绑定
        protected void InfoBind(object sender, StoreRefreshDataEventArgs e)
        {
            string conditions = " where 1=1";
            if (TxtKeys.Text.Trim().Length > 0)
                conditions = string.Format(" where (username like '%{0}%' or firstname like '%{0}%') ", Server.HtmlEncode(TxtKeys.Text.Trim().Replace("'", "")));
            object count = Global.SqlMapper.GetObject("GetuserRowCount",  conditions);
            if (count != null)
                e.Total = Convert.ToInt32(count);
            conditions += " and puserid='" + parentid + "'";
            conditions += string.Format(" limit {0},{1}", e.Start, e.Limit);
            IList<user> list = Global.SqlMapper.GetList<user>(conditions);
            USER_Store.DataSource = list;
            USER_Store.DataBind();
        }
        //获取所有角色，与用户角色ID做映射
        protected void GetAllRoles(object sender, StoreRefreshDataEventArgs e)
        {
            S_Roles.DataSource = Global.SqlMapper.GetList<role>(null);
            S_Roles.DataBind();
        }

        //重置选中用户密码
        protected void BtnResetPwd_Click(object sender, DirectEventArgs e)
        {
            if (Hid.Text.Length <= 0)
            {
                MsgBox.MessageShow("请选择要重置密码的用户", "WARNING"); return;
            }
            user user = Global.SqlMapper.GetOneByKey<user>(Hid.Text);
            user.password = "123456";
            if (Global.SqlMapper.Update<user>(user)==1)
            {
                WinUser.Hide();
                MsgBox.NotifiShow("用户密码重置成功，重置密码为123456", "OK");
                USERINFO_Grid.Reload();
            }
            else
                MsgBox.MessageShow("用户密码重置失败，请重试", "ERROR");
        }
        //删除用户
        protected void BtnDel_Click(object sender, DirectEventArgs e)
        {
            int Success = 0, Failed = 0;
            RowSelectionModel sm = this.USERINFO_Grid.SelectionModel.Primary as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                user user = Global.SqlMapper.GetOneByKey<user>(row.RecordID);
                if (user.username == CookieHelper.GetCookie("username"))
                {
                    MsgBox.MessageShow("系统不允许删除当前登录用户！", "ERROR");
                    continue;
                }
                if (Global.SqlMapper.Delete<user>(user)==1)
                    Success++;
                else
                    Failed++;
            }
            MsgBox.NotifiShow("成功执行了删除，成功删除" + Success.ToString() + "条记录，失败" + Failed.ToString() + "条！", "OK");
            USERINFO_Grid.Reload();
        }
        //保存用户
        protected void BtnSave_Click(object sender, DirectEventArgs e)
        {
            user user = new user();
            if (Hid.Text.Length > 0) {
                user = Global.SqlMapper.GetOneByKey<user>(Hid.Text);
            } else {
                user.password = ("123456");
                user.lastlogintime = DateTime.Now;
                
                user.puserid = parentid;
            }
            user.username = TxtUserName.Text;
            user.roleid = cboRole.SelectedItem.Value;
            user.cell_phone = TxtTel.Text.Trim();
            user.email = TxtEmail.Text.Trim();
            user.address_street = TxtAddress.Text.Trim();
            user.firstname = Txtusernamec.Text.Trim();
            //user.createtime = DateTime.Now.ToString();
            bool newrow = false;
            if (Hid.Text.Length > 0)
            {
                if (Global.SqlMapper.Update<user>(user)==1)
                    MsgBox.NotifiShow("恭喜您，用户信息更新成功！", "OK");
                else
                    MsgBox.MessageShow("用户信息更新失败，请重试！", "ERROR");
            }
            else
            {
                if (Global.SqlMapper.GetList<user>(" where username='" + TxtUserName.Text.Trim() + "'").Count > 0)
                {
                    MsgBox.MessageShow("该客户名称已存在，请使用其他客户名称！", "WARNING");
                    return;
                }
                if (Global.SqlMapper.Create<user>(user) != null) {
                    MsgBox.NotifiShow("用户添加成功，默认密码123456！" + parentid, "OK");
                    newrow = true;
                    
                } else
                    MsgBox.MessageShow("用户添加失败，请重试！", "ERROR");

            }
            WinUser.Hide();
            USERINFO_Grid.Reload();
            X.Js.Call("reloadtree");

        }
        string parentid {

            get {
                string pid = string.Empty;
                pid = string.IsNullOrEmpty(SessionHelper.Get("userpid")) ? "0" : SessionHelper.Get("userpid");
                return pid;
            }
        }
    [DirectMethod(Namespace="App.direct")]
        public void setpid(string pid) {
            SessionHelper.Add("userpid", pid);
            USERINFO_Grid.Reload();
        }
    }
}